#!/bin/bash
#
# $Id: post-uninstall 421 2007-04-05 15:46:55Z dhill $
#
# pre-uninstall steps for columnstore install

running_systemd() {
    if  [ "$(ps --no-headers -o comm 1)" = "systemd" ]; then
        echo 0
    else
        echo 1
    fi
}

if [[ -f /etc/mysql/debian.cnf ]]; then
  MDB="/usr/bin/mysql --defaults-file=/etc/mysql/debian.cnf"
else
  MDB="/usr/bin/mysql"
fi

$MDB 2> ${tmpdir}/mysql_uninstall.log <<EOD
DROP DATABASE IF EXISTS infinidb_querystats;
DELETE FROM mysql.func WHERE dl="ha_columnstore.so";
DELETE FROM mysql.func WHERE dl="libregr_mysql.so";
DELETE FROM mysql.func WHERE dl="libudf_mysql.so";
DROP DATABASE IF EXISTS calpontsys;
DROP PROCEDURE IF EXISTS infinidb_querystats.calSetUserPriority;
DROP PROCEDURE IF EXISTS infinidb_querystats.calRemoveUserPriority(IN host VARCHAR(50), IN usr VARCHAR(50));
DROP PROCEDURE IF EXISTS infinidb_querystats.calShowProcessList;
DROP DATABASE IF EXISTS columnstore_info;
EOD

# Test we are using systemd
systemctl cat mariadb-columnstore.service > /dev/null 2>&1
if [ $? -eq 0 ] && [ $(running_systemd) -eq 0 ]; then
    systemctl stop mariadb-columnstore >/dev/null 2>&1
else
    PROGS='PrimProc ExeMgr DMLProc DDLProc WriteEngineServer StorageManager controllernode workernode'
    kill $(pidof $PROGS) > /dev/null
    sleep 3
    kill -9 $(pidof $PROGS) > /dev/null
    clearShm  > /dev/null 2>&1
fi

if [ -n "$(pgrep -x ProcMon)" ] || [ -n "$(pgrep -x ProcMgr)" ];then
   # Old system must be running, kill ProcMon/ProcMgr
    pkill ProcMon
    pkill ProcMgr
fi

rm -f /etc/profile.d/columnstoreAlias.sh
rm -f @ENGINE_LOGDIR@/*.log1 > /dev/null 2>&1

#get temp directory
tmpDir=`@ENGINE_BINDIR@/mcsGetConfig SystemConfig SystemTempFileDir`
if [[ $tmpDir = *"tmp" ]]; then
	# delete tmp files
	rm -rf $tmpDir/*
fi

rm -f @ENGINE_DATADIR@/local/*.columnstore
rm -rf @ENGINE_DATADIR@/local/etc/
rm -f @ENGINE_DATADIR@/local/moveDbrootTransactionLog
rm -rf @ENGINE_LOGDIR@
rm -rf /tmp/columnstore_tmp_files

lockdir=`@ENGINE_BINDIR@/mcsGetConfig Installation LockFileDirectory`
rm -f $lockdir/columnstore

#uninstall MariaDB Columnstore system logging
columnstoreSyslogSetup.sh uninstall >/dev/null 2>&1

#remove the start service command
systemctl=`which systemctl 2>/dev/null`
if [ -n "$systemctl" ] && [ $(running_systemd) -eq 0 ]; then

    systemctl disable mariadb-columnstore >/dev/null 2>&1
    systemctl disable mcs-controllernode > /dev/null 2>&1
    systemctl disable mcs-ddlproc > /dev/null 2>&1
    systemctl disable mcs-dmlproc > /dev/null 2>&1
    systemctl disable mcs-exemgr > /dev/null 2>&1
    systemctl disable mcs-primproc > /dev/null 2>&1
    systemctl disable mcs-workernode@1 > /dev/null 2>&1
    systemctl disable mcs-workernode@2 > /dev/null 2>&1
    systemctl disable mcs-writeengineserver > /dev/null 2>&1
    systemctl disable mcs-loadbrm > /dev/null 2>&1
    systemctl disable mcs-storagemanager > /dev/null 2>&1

    rm -f /usr/lib/systemd/system/mariadb-columnstore.service
    rm -f /lib/systemd/system/mariadb-columnstore.service
    rm -f /usr/lib/systemd/system/mcs-controllernode.service
    rm -f /lib/systemd/system/mcs-controllernode.service
    rm -f /usr/lib/systemd/system/mcs-ddlproc.service
    rm -f /lib/systemd/system/mcs-ddlproc.service
    rm -f /usr/lib/systemd/system/mcs-dmlproc.service
    rm -f /lib/systemd/system/mcs-dmlproc.service
    if [[ -f  /usr/lib/systemd/system/mcs-exemgr.service ]]; then
        rm -f /usr/lib/systemd/system/mcs-exemgr.service
    fi
    if [[ -f  /lib/systemd/system/mcs-exemgr.service ]]; then
        rm -f /lib/systemd/system/mcs-exemgr.service
    fi
    rm -f /usr/lib/systemd/system/mcs-primproc.service
    rm -f /lib/systemd/system/mcs-primproc.service
    rm -f /usr/lib/systemd/system/mcs-workernode@.service
    rm -f /lib/systemd/system/mcs-workernode@.service
    rm -f /usr/lib/systemd/system/mcs-writeengineserver.service
    rm -f /lib/systemd/system/mcs-writeengineserver.service
    rm -f /usr/lib/systemd/system/mcs-loadbrm.service
    rm -f /lib/systemd/system/mcs-loadbrm.service
    rm -f /usr/lib/systemd/system/mcs-storagemanager.service
    rm -f /lib/systemd/system/mcs-storagemanager.service

    systemctl daemon-reload
    # remove flag to prevent clusters using shared storage from initializing columnstore more than once
    IFLAG=@ENGINE_DATADIR@/storagemanager/storagemanager-lock
    if [ -e $IFLAG ]; then
        rm $IFLAG
    fi
else
    chkconfig=`which chkconfig 2>/dev/null`
    if [ -n "$chkconfig" ]; then
        chkconfig columnstore off > /dev/null 2>&1
        chkconfig --del columnstore > /dev/null 2>&1
        rm -f /etc/init.d/columnstore > /dev/null 2>&1
    else
        updaterc=`which update-rc.d 2>/dev/null`
        if [ -n "$updaterc" ]; then
            update-rc.d -f columnstore remove > /dev/null 2>&1
            rm -f /etc/init.d/columnstore > /dev/null 2>&1
        fi
    fi
fi

#make copy of Columnstore.xml
/bin/cp -f @ENGINE_SYSCONFDIR@/columnstore/Columnstore.xml @ENGINE_SYSCONFDIR@/columnstore/Columnstore.xml.rpmsave > /dev/null 2>&1
/bin/cp -f @ENGINE_SYSCONFDIR@/columnstore/storagemanager.cnf @ENGINE_SYSCONFDIR@/columnstore/storagemanager.cnf.rpmsave > /dev/null 2>&1
/bin/cp -f @MARIADB_MYCNFDIR@/columnstore.cnf @MARIADB_MYCNFDIR@/columnstore.cnf.rpmsave > /dev/null 2>&1
rm -f @ENGINE_SYSCONFDIR@/columnstore/AlarmConfig.xml.installSave

#remove OAMdbrootCheck file
rm -f @ENGINE_DATADIR@/data*/OAMdbrootCheck > /dev/null 2>&1

rm -f /etc/rsyslog.d/49-columnstore.conf > /dev/null 2>&1
rm -rf /tmp/columnstore_tmp_files > /dev/null 2>&1

#tell user to run post configure script
echo " "
echo "MariaDB Columnstore uninstall completed"

exit 0

